Week 3 Engineering Notes: The String Comparison Trap
Section 1: The String Comparison Trap
When searching for a scalar integer within an array, developers evaluate numbers[i] == target. However, executing strings[i] == "David" in bare-metal C induces a catastrophic logical defect by comparing memory addresses rather than the underlying character stream.
===================================================================================
STRING EQUALITY MEMORY TRAP
===================================================================================
if (s == t) ββ> Compares Address 0x100 with Address 0x200 (Result: FALSE!)
if (strcmp(s, t) == 0) ββ> Compares character values 'D','A','V','I','D' (Result: TRUE!)
===================================================================================
- strcmp Routine: To evaluate actual character stream equality, developers must invoke
strcmp, which returns exactly 0 when both strings match perfectly.
Section 2: Applied typedef struct Implementations
To ensure all function prototypes and subroutines recognize the custom structural declaration, it must be instantiated in the global scope prior to main.
- Dot Notation: Developers deploy dot notation
people[i].nameto directly access internal member fields within the struct memory wrapper.
Section 3: Bubble Sort Architectural Optimization
In unoptimized Bubble Sort implementations, the execution loop continues exhaustive comparisons even if the target buffer achieves sorted equilibrium early. To optimize runtime, developers inject a tracking boolean bool swapped.
- Conserving CPU Cycles: If a complete inner loop executes without a single swap, the execution halts instantly, conserving thousands of processor clock cycles.